from xml.sax.handler import ContentHandler
from xml.sax import parse
from xml.dom.minidom import parseString
from random import Random
from changefile import HeadlineHandler
import os, re, string, sys
from create import menuitem

def change(file):
    headlines = []
    if(file != 0):
        print 'it doesnt equal 0'
        file = os.getcwd()+'\\'+file
        parse(file, HeadlineHandler(headlines))
        h = HeadlineHandler(headlines)
        headlines = h.getheadlines()
        print 'got the headlines'
    else:
        print 'it equals 0'
        headlines.append("")
        headlines.append("")
        headlines.append("")
        headlines.append("")
    newfile = changexml(headlines, file)
    return newfile
        
def changexml(headlines, file):
    print 'it got to changexml'
    change = 0
    if (headlines[0] == ""):
        print 'it got to the first command'
        cmd = 'y'
    else:
        print 'The current title is: ' + headlines[0]
        cmd = raw_input('Would you like to change the title? y or n ')
    if(cmd.strip() == 'y'):
        titlecmd = raw_input('What would you like to change the title to? ')
        headlines[0] = titlecmd
        change = 1
        
    if (headlines[1] == ""):
        cmd = 'y'
    else:
        print 'The current heading is: ' + headlines[1]
        cmd = raw_input('Would you like to change the heading? y or n ')
    if(cmd.strip() == 'y'):
        headingcmd = raw_input('What would you like to change the heading to? ')
        headlines[1] = headingcmd
        change = 1
    
    
    if (headlines[2] == ""):
        cmd = 'y'
    else:
        print 'The current sub-heading is: ' + headlines[2]
        cmd = raw_input('Would you like to change the sub-heading ? y or n ')
    if(cmd.strip() == 'y'):
        subheadcmd = raw_input('What would you like to change the sub-heading to? ')
        headlines[2] = subheadcmd
        change = 1
    
    
    if (headlines[3] == ""):
        cmd = 'y'
    else:
        print 'The current content is: ' + headlines[3]
        cmd = raw_input('Would you like to change the content? y or n ')
    if(cmd.strip() == 'y'):
        contentcmd = raw_input('What would you like to change the content to? ')
        headlines[3] = contentcmd
        change = 1
        
    if(change == 1):
        #START CREATION OF DOCUMENT WITH DOM
        # create a new document
        doc = parseString(u'<page/>'.encode('UTF-8'))
        
        pagedata = doc.documentElement
        # create a sect1 element
        s1 = doc.createElementNS(None,u'pagedata')
        # add it under the root element
        pagedata.appendChild(s1)
        # create a title element with a text node inside
        s1.appendChild(doc.createElementNS(None,u'title'))
        title = doc.createTextNode(headlines[0])
        s1.firstChild.appendChild(title)
        
        heading = doc.documentElement
        # create a sect1 element
        s2 = doc.createElementNS(None,u'heading')
        # add it under the root element
        heading.appendChild(s2)
        txt = doc.createTextNode(headlines[1])
        s2.appendChild(txt)
        
        subhead = doc.documentElement
        # create a sect1 element
        s3 = doc.createElementNS(None,'subhead')
        # add it under the root element
        subhead.appendChild(s3)
        txt = doc.createTextNode(headlines[2])
        s3.appendChild(txt)
        
        content = doc.documentElement
        # create a sect1 element
        s4 = doc.createElementNS(None,'content')
        # add it under the root element
        content.appendChild(s4)
        txt = doc.createTextNode(headlines[3])
        s4.appendChild(txt)
        
        # write the result
        s = doc.toprettyxml()
        if(file == 0):
            rand = Random()
            r = rand.randrange(100000);
            rf = str(r)+'.xml'
            file = rf
            print 'going to change menu'
            menuitem(file)
            print 'changed menu successfully'
        f = open(file, 'w')
        f.write(s)
        f.close()
        print 'The following are your requested changes: '
        print 'Title: ' + headlines[0]
        print 'Heading: ' + headlines[1]
        print 'SubHeading: ' + headlines[2]
        print 'Content: ' + headlines[3]
        return file
    else:
        return 0
